from django.core.paginator import Paginator


class LxgPageNation:
    # def __init__(self, request, content_list, page_size=20):
    def __init__(self, request, ModelObject, page_size=20):
        """
        content_list: 要进行分页的内容列表
        page_size: 每页内容数量
        """
        self.page_num = int(request.GET.get('p', 1))  # 当前页
        self.page_size = int(request.GET.get('s', page_size))  # 每页数量
        self.content_list = ModelObject.objects.all().order_by("-id")
        # self.paginator = Paginator(content_list, self.page_size)  # 分页对象
        self.paginator = Paginator(self.content_list, self.page_size)  # 分页对象
        self.page = self.paginator.page(self.page_num)  # 当前页

    def get_middle_range(self, arr, num):
        """传入页码数组和页码-1, 得到该页对应的页码"""
        # 如果数组长度小于等于5, 就直接返回整个数组
        if len(arr) <= 5:
            return arr

        max_index = len(arr) - 1
        # 动态指定索引
        index1 = None
        index2 = None
        index3 = None
        index4 = None
        index5 = None
        # 9 其实是请求第10页
        # 目标: 8 9 10 1 2

        # 9
        # 最后五个索引:                max_index-1      max_index
        # max_index - 2 7
        if num >= max_index - 2:
            index1 = max_index - 4
            index2 = max_index - 3
            index3 = max_index - 2
            index4 = max_index - 1
            index5 = max_index
        # 6
        # 目标: 5 6 7 8 9
        # 目标索引: 4 5 6 7 8
        elif 2 < num < max_index - 2:
            index1 = num - 2
            index2 = num - 1
            index3 = num
            index4 = num + 1
            index5 = num + 2
        # 2
        # 目标: 1 2 3 4 5
        # 目标索引: 0 1 2 3 4
        elif num <= 2:
            index1 = 0
            index2 = 1
            index3 = 2
            index4 = 3
            index5 = 4

        return [
            arr[index1],
            arr[index2],
            arr[index3],
            arr[index4],
            arr[index5],
        ]

    def get_page(self, page_num=1):
        """
        获取分页
        page_num: 页码
        """
        self.page = self.paginator.page(page_num)
        # 添加到字典对象
        context = {
            "object_list": self.page.object_list,  # 文章列表
            "page": self.page,  # 分页对象
            "middle_page": self.get_page(page_num),  # 中间页页码
            "current_page": int(page_num)  # 当前页
        }
        return context

    def get_objects(self):
        """获取分页列表"""
        return self.page.object_list

    def get_page_html(self):
        """
        获取分页html
        page_num: 页码
        """
        html = ""
        html += """<nav aria-label="Page navigation">"""
        html += """<ul class="pagination">"""
        # 上一页
        if self.page.has_previous():
            html += """<li>
                        <a href="?p={}" aria-label="Previous">
                            <span aria-hidden="true">上一页</span>
                        </a>
                    </li>""".format(self.page.previous_page_number())
        else:
            html += """<li class="disabled">
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">上一页</span>
                        </a>
                    </li>"""
        # 中间页
        for middle in self.get_middle_range(list(self.paginator.page_range), self.page_num):
            if middle == self.page_num:
                html += """<li class="active"><a href="?p={}">{}</a></li>""".format(middle, middle)
            else:
                html += """<li><a href="?p={}">{}</a></li>""".format(middle, middle)
        # 下一页
        if self.page.has_next():
            html += """<li>
                        <a href="?p={}" aria-label="Next">
                            <span aria-hidden="true">下一页</span>
                        </a>
                    </li>""".format(self.page.next_page_number())
        else:
            html += """<li class="disabled">
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">下一页</span>
                        </a>
                    </li>"""
        html += """</ul>"""
        html += """</nav>"""
        return html
